package servlet;

import bean.NewsofUser;
import bean.User;
import dao.NewsDao;
import utils.ImgUtil;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@WebServlet("/news.do")
public class NewsPubServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public NewsPubServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        request.setCharacterEncoding("UTF-8");
//        response.setCharacterEncoding("UTF-8");

        String newTitle = request.getParameter("newsTitle");
        String newTitle1 = request.getParameter("newsTitle1");
        String html = request.getParameter("html");
        User user = (User) request.getSession().getAttribute("user");
//            System.out.println(user.getId() + "用户主键");
        String userid = String.valueOf(user.getId());
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter mysqlFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String mysqlDateTimeFormat = now.format(mysqlFormatter);
        if(newTitle1!=null){
            ImgUtil imgUtil = new ImgUtil(html);
            String headimg=imgUtil.deal();
            Integer newid = (Integer) request.getSession().getAttribute("newid");
            if (NewsDao.Update1(newid,newTitle1, html, userid, mysqlDateTimeFormat, headimg)) {
                List<NewsofUser> list = NewsDao.select();
                //request.setAttribute("list", list);
                request.getSession().setAttribute("list", list);
                response.sendRedirect("/press_release_system_war_exploded/testlimit"); // Redirect to welcome page

                response.getWriter().print("<script>alert('succesd');location.href='jsp/publish.jsp';</script>");
            } else {
                response.getWriter().print("<script>alert('fail');location.href='jsp/publish.jsp';</script>");
            }
        }else {
            ImgUtil imgUtil = new ImgUtil(html);
            String headimg=imgUtil.deal();
            if (NewsDao.Insert(newTitle, html, userid, mysqlDateTimeFormat, headimg)) {
                List<NewsofUser> list = NewsDao.select();
                //request.setAttribute("list", list);
                request.getSession().setAttribute("list", list);
                response.sendRedirect("/press_release_system_war_exploded/testlimit"); // Redirect to welcome page

                response.getWriter().print("<script>alert('succesd');location.href='jsp/publish.jsp';</script>");
            } else {
                response.getWriter().print("<script>alert('fail');location.href='jsp/publish.jsp';</script>");
            }

        }


    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
